package org.redis.cluster;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

public class OrderSorter implements Comparator<Object> {
	
	private final static Comparator<Object> INSTANCE=new OrderSorter();
	
	public static void sort(List<?> list) {
		if(CollectionUtils.isEmpty(list)) {
			return;
		}
		
		Collections.sort(list, INSTANCE);
	}

	@Override
	public int compare(Object o1, Object o2) {
		if(!(o1 instanceof Ordered) && !(o2 instanceof Ordered)) {
			return 0;
		}
		
		if((o1 instanceof Ordered) && !(o2 instanceof Ordered)) {
			return -1;
		}
		
		if(!(o1 instanceof Ordered) && (o2 instanceof Ordered)) {
			return 1;
		}

		return ((Ordered)o1).getOrder()-((Ordered)o2).getOrder();
	}
}